<template>
  <div class="topic-modal-mask">
    <div class="editor__model-frame">
      <!-- 头部 -->
      <div class="editor__model-frame--header">
        选择社区
        <button class="editor__model-frame--close" @click="closeModal">
          <i class="hb-icon heybox-common_close_line_24x24"></i>
        </button>
      </div>

      <!-- 主体内容 -->
      <div class="editor-model__topic">
        <!-- 话题列表 -->
        <div class="editor-model__topic-list">
          <div
            v-for="(topic, index) in filteredTopics"
            :key="index"
            class="editor-model__topic-list-item"
            :class="{ selected: isTopicSelected(topic) }"
            @click="selectTopic(topic)"
          >
            <div class="topic-list-item__content">
              <div class="topic-list-item__title">
                #{{ topic.topicName }}
                <span v-if="isTopicSelected(topic)" class="selected-tag"
                  >(已选择)</span
                >
              </div>
            </div>
          </div>

          <!-- 集成取消按钮 -->
          <div
            class="editor-model__topic-list-item cancel-item"
            @click="closeModal"
          >
            <div class="topic-list-item__content">
              <div class="topic-list-item__title cancel-text">取消选择</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getAllTopic } from '@/apis/topic.js' // 引入API方法

// 声明 emit
const emit = defineEmits(['close', 'add-topic'])

const searchKeyword = ref('')
const topicList = ref([]) // 用于存储从后端获取的主题列表

// 计算属性：过滤话题
const filteredTopics = computed(() => {
  return topicList.value.filter((topic) =>
    topic.topicName.toLowerCase().includes(searchKeyword.value.toLowerCase())
  )
})

// 获取话题数据
const fetchTopics = async () => {
  try {
    const res = await getAllTopic() // 从API获取话题数据
    if (res.code === '200') {
      topicList.value = res.data // 更新话题列表
    } else {
      console.error('获取话题失败:', res.message || '未知错误')
    }
  } catch (error) {
    console.error('获取话题失败:', error)
  }
}

// 生命周期钩子：组件加载时获取话题数据
onMounted(() => {
  fetchTopics()
})

const closeModal = () => {
  emit('close')
}

const props = defineProps({
  selectedTopics: {
    type: Array,
    default: () => [],
  },
})

const isTopicSelected = (topic) => {
  return props.selectedTopics.some((t) => t.topicId === topic.topicId)
}

// 点击时检查是否已选
const selectTopic = (topic) => {
  if (!isTopicSelected(topic)) {
    emit('add-topic', topic)
  }
  closeModal()
}
</script>

<style scoped>
.topic-modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.editor__model-frame {
  background: #fff;
  border-radius: 12px;
  width: 480px;
  max-height: 80vh;
  overflow: hidden;
  animation: modalSlide 0.3s ease;
}

@keyframes modalSlide {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.editor__model-frame--header {
  position: relative;
  padding: 20px;
  font-size: 18px;
  font-weight: 600;
  border-bottom: 1px solid #eee;
  text-align: left;
}

.editor__model-frame--close {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
}

.editor-model__search-wrapper {
  padding: 16px 20px;
}

.editor__search-input {
  display: flex;
  align-items: center;
  background: #f5f5f5;
  border-radius: 8px;
  padding: 8px 12px;
}

.editor__search-input--input {
  flex: 1;
  border: none;
  background: none;
  padding: 4px;
  font-size: 14px;
}

.editor-model__topic-list {
  max-height: 400px;
  overflow-y: auto;
  padding: 0 20px 20px;
}

.editor-model__topic-list-item {
  display: flex;
  align-items: center;
  padding: 12px;
  margin: 8px 0;
  border-radius: 8px;
  background: #f8f9fa;
  cursor: pointer;
  transition: all 0.2s;
}

.editor-model__topic-list-item:hover {
  background: #f1f3f5;
}

.topic-list-item__content {
  display: flex;
  flex-direction: column;
}

.topic-list-item__title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.topic-list-item__desc {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

/* 取消按钮样式 */
.cancel-item {
  background: none !important;
  border: 2px solid #007bff;
  justify-content: center;
  margin-top: 20px;
}

.cancel-item:hover {
  background: #f0f8ff !important;
}

.cancel-text {
  color: #007bff !important;
  font-weight: 600 !important;
}

.selected {
  background: #e6f7ff !important;
  border: 1px solid #91d5ff !important;
}

.selected-tag {
  color: #1890ff;
  font-size: 12px;
  margin-left: 4px;
}
</style>
